using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GSBServiceListBox;
using System.Reflection;
using System.Xml;
using System.IO;
using System.Collections;
using System.ServiceModel;
using System.Runtime.Serialization;
using UIIronTextBox;


/******
 *
    Copyright 2008 Mitch Barnett
	This file is part of Global System Builder.

    Global System Builder is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Global System Builder is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Global System Builder.  If not, see <http://www.gnu.org/licenses/>.
 *
******/

namespace GSBService
{
    public partial class GSBServiceForm : Form
    {
        //MNB Added - this HashTable holds onto the name of the assembly as
        // a key and the value is the assembly instance
        public static Hashtable gsbGlobalAssemblyHashtable;

        public static UIIronTextBox.IronTextBoxControl IPTextBox;

        //private variable for the service host
        private ServiceHost sh;


        public GSBServiceForm()
        {
            
            InitializeComponent();
            //MNB code
            assemblyListBox.SelIndexChanged += new SelectedIndexDeleg(assemblyListBox_SelIndexChanged);

            IPTextBox = ironTextBoxControl1;
            
        }

        private void assemblyListBox_SelIndexChanged(object Item, int ItemIndex)
        {
            // dummy stuff to test getting the name of the assembly and then getting its types
            // the WCF implementation would pass the assembly name
            //string myTypeName = Item.ToString();
            //Assembly myAssembly = (Assembly)gsbGlobalAssemblyHashtable[myTypeName];
        }


        [STAThread]

        private void GSBServiceForm_Load(object sender, EventArgs e)
        {

            //start the service up
            Application.DoEvents();

            this.sh = new ServiceHost(typeof(GSBService));
            this.sh.Open();


            assemblyListBox.IconImageList = imageList1;

            //TODO:  try catch block on I/O
            //TODO:  put in path of exe
            StreamReader sr = new StreamReader("C:\\projects\\gsbservice\\Test.txt");

            string sLine = "";

            ArrayList arrText = new ArrayList();

            //Continue to read until you reach end of file
            while (sLine != null)
            {

                sLine = sr.ReadLine();
                if (sLine != null)
                    arrText.Add(sLine);
            }

            //close the file
            sr.Close();

            // new Hashtable to add the assmbly names and instances
            Hashtable gsbHashtable = new Hashtable();

            foreach (string sOutput in arrText)
                if (sOutput != "")
                {
                    //TODO: Need to catch if the LoadFrom fails
                    Assembly myAssembly = Assembly.LoadFrom(sOutput);
                    assemblyListBox.AddItem(myAssembly.GetName().Name, 0, Color.Black);
                    gsbHashtable.Add((myAssembly.GetName().Name), myAssembly);
                }
            // assign the local hashtable to the global
            gsbGlobalAssemblyHashtable = gsbHashtable;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            assemblyListBox.IconImageList = imageList1;
            //OpenFileDialog openFileDialog1 = new OpenFileDialog();

            this.openFileDialog1.InitialDirectory = "c:\\";
            this.openFileDialog1.Filter = "Assembly files (*.dll,*.exe,*.mcl)|*.dll|*.exe|*.mcl|All files (*.*)|*.*";
            this.openFileDialog1.FilterIndex = 1;
            this.openFileDialog1.RestoreDirectory = true;

            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string assemblyString = this.openFileDialog1.FileName;
                    // need to write the filename to a cfg file
                    //check for duplicates, blah, blah
                    //need a global array here to keep adding assemblies

                    //TODO:  Try catch on LoadFrom
                    Assembly myAssembly = Assembly.LoadFrom(assemblyString);

                    assemblyListBox.AddItem(myAssembly.GetName().Name, 0, Color.Black);

                    // add the newly opened item to the global Hashtable
                    gsbGlobalAssemblyHashtable.Add((myAssembly.GetName().Name), myAssembly);

                    //TODO: try catch on file I/O
                    //Pass the filepath and filename to the StreamWriter Constructor
                    StreamWriter sw = new StreamWriter("C:\\projects\\gsbservice\\Test.txt", true, Encoding.ASCII);

                    //Write a line of text
                    sw.WriteLine(assemblyString);

                    //Close the file
                    sw.Close();
                }

                catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

            }
            else

                MessageBox.Show("The cancel button was clicked or Esc was pressed");

        }

        private void fileToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //TODO: this is just a stub to test the execution of a Python script.  Needs to be removed.

            string str = "c:\\projects\\gsbservice\\simple.py";
            string result = "";

            result = ""; //gblInteractive.ExecuteIPFile(str);

            MessageBox.Show(result);
        }

        public string IPCommand(string cmd) 
        {
            string test;
         
            test = ironTextBoxControl1.ExecuteIPCommand(cmd);

            return test;

        }

        
    }
}